import * as type from "./mutation-types.js";
import {playMode} from "commom/js/config.js";
import {shuffle} from "commom/js/until.js";
import {saveSearch,deleteSearch,clearSearch,savePlay,deletePlay,clearPlay} from "commom/js/catch.js";
export const selectPlay = function({commit,state},{list,index}){
  commit(type.SET_SEQUENCE_LIST,list);
  if(state.playMode === playMode.random){//处于随机播放的状态后在点击其他歌曲的操作
    let randomList = shuffle(list);
    commit(type.SET_PLAYLIST,randomList);
    index = findIndex(randomList,list[index])
  }else{
    commit(type.SET_PLAYLIST,list);
  }
  commit(type.SET_PLAYING_STATE,true)
  commit(type.SET_FULL_SCREEN,true)
  commit(type.SET_CURRENT_INDEX,index)
}

function findIndex(list, song) {
  return list.findIndex((item) => {
    return item.id === song.id
  })
}
export const insertSong = function({commit,state},song){
  let playList = state.playList.slice();
  let sequenceList = state.sequenceList.slice();
  let currentIndex = state.currentIndex;
  let currentSong = playList[currentIndex];
  let fpIndex = findIndex(playList,song);
  currentIndex++;
  playList.splice(currentIndex,0,song);
  if(fpIndex > -1){
    if(currentIndex > fpIndex){
      playList.splice(fpIndex,1)
      currentIndex--
    }else{
      playList.splice(fpIndex+1,1)
    }
  }
  let currentSIndex = findIndex(sequenceList,currentSong)+1
  let fsIndex = findIndex(sequenceList,song);
  sequenceList.splice(currentSIndex,0,song);
  if(fsIndex > -1){
    if(currentSIndex > fsIndex){
      sequenceList.splice(fsIndex,1)
    }else{
      sequenceList.splice(fsIndex+1,1)
    }
  }
  commit(type.SET_PLAYLIST,playList);
  commit(type.SET_SEQUENCE_LIST,sequenceList);
  commit(type.SET_CURRENT_INDEX,currentIndex);
  commit(type.SET_PLAYING_STATE,true)
  commit(type.SET_FULL_SCREEN,true)
}
export const saveSearchHistory = function({commit},querry){
  commit(type.SET_SEARCH_HISTORY,saveSearch(querry))
}
export const deleteSearchHistory = function({commit},querry){
  commit(type.SET_SEARCH_HISTORY,deleteSearch(querry))
}
export const clearSearchHistory = function({commit},querry){
  commit(type.SET_SEARCH_HISTORY,clearSearch(querry))
}

export const deleteSong = function({commit,state},song){
  let playlist = state.playList.slice()
  let sequencelist = state.sequenceList.slice()
  let currentindex = state.currentIndex
  let pindex = findIndex(playlist,song)
  playlist.splice(pindex,1)
  let sindex = findIndex(sequencelist,song)
  sequencelist.splice(sindex,1)
  if(currentindex > pindex || currentindex === playlist.length){
    currentindex--
  }
  commit(type.SET_PLAYLIST,playlist);
  commit(type.SET_SEQUENCE_LIST,sequencelist);
  commit(type.SET_CURRENT_INDEX,currentindex);
  if(playlist.length === 0){
    commit(type.SET_PLAYING_STATE,false);
  }else{
    commit(type.SET_PLAYING_STATE,true);
  }
}
export const deleteSongList = function({commit}){
  commit(type.SET_PLAYLIST,[]);
  commit(type.SET_SEQUENCE_LIST,[]);
  commit(type.SET_CURRENT_INDEX,-1);
  commit(type.SET_PLAYING_STATE,false);
}
export const savePlayHistory = function({commit},song){
  commit(type.SET_PLAY_HISTORY,savePlay(song))
}
export const deletePlayHistory = function({commit},song){
  commit(type.SET_PLAY_HISTORY,deletePlay(song))
}
export const clearPlayHistory = function({commit},song){
  commit(type.SET_PLAY_HISTORY,clearPlay(song))
}